*
* COVARIATE BALANCE
*
set varabbrev off, permanently
clear all
use epfipc2006-2019plus.dta
*
global listtotal gasto1 gasto2 gasto3 gasto4 gasto5 gasto6 gasto7 gasto8 gasto9 gasto10 gasto11 gasto12 ///
       income squaredm othehouse residshort ///
	   ttenure1 ttenure2 ttenure3 ttenure4 ttenure5 ttenure6 pdensity1 pdensity2 pdensity3 ///
	   tsize1 tsize2 tsize3 tsize4 tsize5 nmembers nfemales ///
	   nforeign ndworkers nactive nincome n0_4 n5_15 n16_24 n25_34 n35_64 n65_79 n80omas ///
	   nstudies1 nstudies2 nstudies3 nstudies4 ///
	   labinc peninc uneinc capinc agemb femalemb foreignmb unemployedmb inactivemb

global listpartial gasto1-gasto12 year2-year13 ccaa2-ccaa18 income squaredm othehouse ///
       residshort ttenure2-ttenure6 pdensity2-pdensity3 tsize2-tsize5 nmembers nfemales ///
	   nforeign ndworkers nactive nincome n5_15 n16_24 n25_34 n35_64 n65_79 n80omas nstudies2-nstudies4 ///
	   labinc peninc uneinc capinc agemb femalemb foreignmb unemployedmb inactivemb
*
* Total expenditure
*
foreach var1 in T01 T12 T10 T21 {
	preserve
	keep if post==0 & `var1'!=.
	* propensity score estimation with survey weights
	logit `var1' $listpartial  [pweight=factor]
	predict double ps if e(sample)==1, pr
	drop if ps==.
	gen w=(`var1'+(((1-`var1')*ps)/(1-ps)))
	* Weights used for ATET estimation accounting for survey weights
	gen estweight=w*factor
	* imposing common support
	sum ps if `var1'==1
	scalar pmintreat=r(min)
	drop if ps<pmintreat & `var1'==0
	sum ps if `var1'==0
	scalar pmaxuntreat=r(max)
	drop if ps>pmaxuntreat & `var1'==1
	* estimating ATET for total expenditure
	reg dgastot `var1' [aweight=estweight],r
	drop if e(sample)==0
	* Unweighted
	dis "UNWEIGHTED"
	foreach var in $listtotal {
		quietly sum `var' if `var1'==1
		quietly scalar m1=r(mean)
		quietly scalar s1=r(sd)
		quietly sum `var' if `var1'==0
		quietly scalar m0=r(mean)
		quietly scalar s0=r(sd)
		quietly scalar smd=(m1-m0)/(sqrt(((s1^2)+(s0^2))/2))
		quietly scalar vr=(s1^2)/(s0^2)
		* dis "smd =    ", %9.2f smd, "              vr =.               ", %9.2f vr, "       `var'"
		dis " ", %9.2f smd, " ", %9.2f vr, "       `var'"
	}
	dis "WEIGHTED"
	foreach var in $listtotal {
		quietly sum `var' [iweight=estweight] if `var1'==1
		quietly scalar m1=r(mean)
		quietly scalar s1=r(sd)
		quietly sum `var' [iweight=estweight] if `var1'==0
		quietly scalar m0=r(mean)
		quietly scalar s0=r(sd)
		quietly scalar smd=(m1-m0)/(sqrt(((s1^2)+(s0^2))/2))
		quietly scalar vr=(s1^2)/(s0^2)
		*dis "smd =    ", %9.2f smd, "              vr =.               ", %9.2f vr, "       `var'"
		dis " ", %9.2f smd, " ", %9.2f vr, "       `var'"
	}
restore
}

*
* The End
*
